package com.atguigu.guli.service.sms.controller.api;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.common.base.util.FormUtils;
import com.atguigu.guli.common.base.util.RandomUtils;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.sms.service.SmsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

//@CrossOrigin
@Api("短信管理")
@RequestMapping("/api/sms")
@RestController
@Slf4j
public class ApiSmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("send/{mobile}")
    public R getCode(@PathVariable("mobile") String mobile){

        // 检验手机号是否合法
        if(StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile)){
            log.error("手机号不正确");
           throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
        }

        // 生成验证码
        String checkCode = RandomUtils.getFourBitRandom();
        // 发送验证码
        boolean send = smsService.send(mobile, checkCode);
        if(!send){
            log.error("发送失败");
            return  R.error().message("发送失败").code(32323);
        }
        // 存储验证码到redis
        redisTemplate.opsForValue().set(mobile,checkCode, 5,TimeUnit.MINUTES);

        return R.ok();
    }


}
